= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 A u t h o r :   Y o u n g w o o k   K i m   ( K o r e a n ) 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 C o n t a c t :   r u m i a 0 6 0 1 @ g m a i l . c o m 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 B a s i c   T E M P L A T E   a n d   O U T P U T 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
  I n t o   E v e n t - d r i v e n   a n d   G U I 
 
 = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
 
 A s   I   s a i d ,   P y g a m e   i s   b a s e d   o n   G U I   e n v i r o n m e n t .   F u r t h e r m o r e ,   P y g a m e   i s   g o o d   f o r   m a k i n g   2 D   g a m e   b e c a u s e   o f   i t s   i n p u t / o u t p u t   f o r m a t .   S o ,   y o u   h a v e   t o   s a y   g o o d - b y e   f o r   p r i n t   o r   i n p u t   s t a n d a r d   f u n c t i o n   o f   P y t h o n   ( B e c a u s e   t h e y   w o r k   o n l y   o n   C U I   e n v i r o n m e n t ) .   T h e n ,   w h a t   f u n c t i o n s   i n   P y g a m e   r e p l a c e   t h e s e   f u n c t i o n s ?   F i r s t ,   w e   h a v e   t o   g o   b a c k   t o   f r i e n d l y    H e l l o   W o r l d !    p r o j e c t ,   w h i c h   i s   l e a r n i n g   a b o u t   b a s i c   t e m p l a t e   a n d   o u t p u t .   * * ( R e q u i r i n g   a n y   f o n t   f i l e ( . t t f )   i n   t h e   s a m e   p r o j e c t   d i r e c t o r y ) * * 
 
 
 
 
 
 . .   i m a g e : :   B a s i c - o u p u t - s o u r c e c o d e . p n g 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   2 2 0 ,   1 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " B a s i c - o u p u t - s o u r c e c o d e . p n g " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 
 
 . .   i m a g e : :   B a g i c - o u p u t - r e s u l t - s c r e e n . p n g 
 
       : c l a s s :   i n l i n e d - r i g h t 
 
 
 
 . .   c o d e - b l o c k : :   p y t h o n 
 
       : l i n e n o s : 
 
 
 
       i m p o r t   s y s ,   p y g a m e 
 
       p y g a m e . i n i t ( ) 
 
 
 
       s i z e   =   w i d t h ,   h e i g h t   =   2 2 0 ,   1 4 0 
 
       s p e e d   =   [ 2 ,   2 ] 
 
       b l a c k   =   0 ,   0 ,   0 
 
 
 
       s c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( s i z e ) 
 
 
 
       b a l l   =   p y g a m e . i m a g e . l o a d ( " B a g i c - o u p u t - r e s u l t - s c r e e n . p n g " ) 
 
       b a l l r e c t   =   b a l l . g e t _ r e c t ( ) 
 
 
 
       w h i l e   1 : 
 
               f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) : 
 
                       i f   e v e n t . t y p e   = =   p y g a m e . Q U I T :   s y s . e x i t ( ) 
 
 
 
               b a l l r e c t   =   b a l l r e c t . m o v e ( s p e e d ) 
 
               i f   b a l l r e c t . l e f t   <   0   o r   b a l l r e c t . r i g h t   >   w i d t h : 
 
                       s p e e d [ 0 ]   =   - s p e e d [ 0 ] 
 
               i f   b a l l r e c t . t o p   <   0   o r   b a l l r e c t . b o t t o m   >   h e i g h t : 
 
                       s p e e d [ 1 ]   =   - s p e e d [ 1 ] 
 
 
 
               s c r e e n . f i l l ( b l a c k ) 
 
               s c r e e n . b l i t ( b a l l ,   b a l l r e c t ) 
 
               p y g a m e . d i s p l a y . f l i p ( ) 
 
 
 
 
 
 ( S o u r c e   C o d e   f o r   H e l l o   W o r l d   P r o j e c t   a n d   i t s   r e s u l t   s c r e e n ) 
 
 
 
 W o w ,   i t  s   c o m p l i c a t e d   i n   c o m p a r e   t o   p y t h o n  s   p r i n t (  H e l l o   W o r l d  )   ( J u s t   a   s i n g l e   l i n e   c o m m a n d ) .   I t  s   b e c a u s e   t e x t   i n   G U I   e n v i r o n m e n t   h a s   5   c o m p o n e n t s :   t e x t   c o n t e n t s ,   f o n t ,   s i z e ,   c o l o r   a n d   l o c a t i o n .   I f   y o u   w a n t   t o   p r i n t   a n y   t e x t   i n t o   s c r e e n ,   y o u   h a v e   t o   s e t   o t h e r   4   c o m p o n e n t s ,   n o t   o n l y   s t r i n g   ( u n l e s s   w h e n   i t   i s   i n   G U I   e n v i r o n m e n t ) .   O n l y   ` ` p y g a m e . d i s p l a y . s e t _ c a p t i o n (  H e l l o   W o r l d   P r o j e c t  ) ` `   f u n c t i o n   i n   # 7   d o   t h e   s a m e   f u n c t i o n   a s   p y t h o n  s   p r i n t (  H e l l o   W o r l d   P r o j e c t  ) .   T h e   o n l y   d i f f e r e n c e   i s   t h a t   o u t p u t   s t r i n g   i s   a l w a y s   o n   w i n d o w   c a p t i o n   ( t i t l e   o f   c u r r e n t   p r o g r a m ) 
 
 
 
 F i r s t ,   l o o k   a t   t h e   t e m p l a t e   o f   s o u r c e   c o d e   b e f o r e   u n d e r s t a n d i n g   h o w   t o   o u t p u t   s o m e t h i n g ,   s o u r c e   c o d e   c a n   b e   d i v e d   i n t o   4   s e c t i o n s :   H e a d e r ( # 1 - # 2 ) ,   I n i t i a l   s t a t e m e n t ( # 3 - # 1 2 ) ,   A l w a y s   s t a t e m e n t ( # 1 3 - # 2 0 )   a n d   E v e n t   s t a t e m e n t ( # 1 6 - # 1 9 ) . 
 
 
 
 I n   * * H e a d e r * * ,   i m p o r t i n g   m o d u l e s   w i l l   b e   e x e c u t e d .   ` ` i m p o r t   p y g a m e ,   s y s ` `   i s   a l w a y s   n e e d e d .   N e e d l e s s   t o   s a y ,   b e c a u s e   t h i s   i s   p y g a m e   p r o j e c t   a n d   g a m e   h a s   t o   b e   t e r m i n a t e d   w h e n   p l a y e r   w a n t   t o   e x i t ( ` ` s y s . e x i t ( ) ` `   a t   # 1 9 ) .   ` ` f r o m   p y g a m e . l o c a l s   i m p o r t * ` `   i s   a l s o   n e c e s s a r y   t o   u s e   u s e f u l   c o n s t a n t s   j u s t   l i k e   ` ` Q U I T ` `   a t   # 1 7 . 
 
 I n   * * I n i t i a l   s t a t e m e n t * *   ( c o m m a n d s   b e f o r e   i n f i n i t e   l o o p ) ,   s o m e   g l o b a l   v a l u e s   w i l l   b e   i n i t i a l i z e d   a n d   s o m e   f u n c t i o n s   w i l l   b e   c a l l e d   f o r   o n e   t i m e .   G l o b a l   v a l u e s   j u s t   l i k e   c o l o r s   h a s   t o   b e   i n i t i a l i z e d   h e r e   i n   o r d e r   t o   i n c r e a s e   r e a d a b i l i t y .   R e m e m b e r ,   t h i s   i s   G U I   w h i c h   i s   c o l o r f u l .   C o l o r   h a s   t h r e e   c o m p o n e n t s :   r e d ,   g r e e n   a n d   b l u e .   S o ,   c o l o r   v a l u e   h a s   t o   b e   i n i t i a l i z e d   j u s t   l i k e   ` ` r e d   =   ( 2 5 5 ,   0 ,   0 ) ` ` .   F u n c t i o n   n a m e d   ` ` p y g a m e . i n i t ( ) ` `   m u s t   b e   c a l l e d   p r e c e d i n g   t o   o t h e r   p y g a m e   f u n c t i o n s .   T h e n   o t h e r   p y g a m e   f u n c t i o n s   c a n   b e   e x e c u t e d .   ( O t h e r   p y g a m e   f u n c t i o n s   w i l l   b e   e x p l a i n e d   l a t e r . ) 
 
 
 
 I n   * * A l w a y s   s t a t e m e n t * *   ( c o m m a n d s   w i t h i n   i n f i n i t e   l o o p ) ,   s o m e   g l o b a l   v a l u e s   w i l l   b e   u p d a t e d   r o u t i n e l y   a n d   s o m e   f u n c t i o n s   w i l l   b e   c a l l e d   r o u t i n e l y   u n l e s s   t h e y   a r e   e n c l o s e d   i n t o   c o n d i t i o n a l   s t a t e m e n t .   F u n c t i o n   n a m e d   ` ` p y g a m e . d i s p l a y . u p d a t e ( ) ` `   s h o u l d   b e   c a l l e d   a f t e r   e v e r y   p r o c e s s e s   a r e   d o n e .   B e c a u s e   t h i s   f u n c t i o n   i s   p r i n t i n g   t h e   r e s u l t s   o f   p r o c e s s e s   o n t o   s c r e e n ( = m o n i t o r ) .   I f   t h i s   f u n c t i o n   i s   n o t   e x e c u t e d   i n   t h e   l a s t   p a r t   o f   A l w a y s   s t a t e m e n t ,   t h e r e   w i l l   b e   a   p r o b a b i l i t y   t h a t   c u r r e n t   s c r e e n   a n d   i n t e r n a l   d a t a   d o e s   n o t   m a t c h .   ( O t h e r   p y g a m e   f u n c t i o n s   w i l l   b e   e x p l a i n e d   l a t e r . ) 
 
 
 
 I n   * * E v e n t   s t a t e m e n t * *   ( c o m m a n d s   w i t h i n   l o o p   w h i c h   c h e c k   e v e r y   p o s s i b l e   e v e n t s ) ,   t h e r e   w i l l   b e   s u i t a b l e   c o n d i t i o n a l   s t a t e m e n t s   w h e n   c e r t a i n   e v e n t   i s   t r i g g e r e d .   ` ` p y g a m e . e v e n t . g e t ( ) ` `   f u n c t i o n   r e t u r n s   a   l i s t   o f   e v e n t s   o c c u r r e d   b y   p r o c e s s e s   i n   A l w a y s   s t a t e m e n t .   A n d   t h i s   l i s t   i s   a u t o m a t i c a l l y   a r r a n g e d   b y   t i m e   ( o l d e s t   t o   n e w e s t ) .   S o ,   u s i n g   f o r - i n   s t a t e m e n t ,   e v e r y   t r i g g e r e d   e v e n t   c a u s e d   b y   A l w a y s   s t a t e m e n t   c a n   b e   r e s o l v e d   p r o c e d u r a l l y .   ( R e m i n d   t h a t   t h i s   i s   t h e   t r a i t   o f   e v e n t - d r i v e n . )   F o r   e x a m p l e ,   c o m m a n d s   i n   # 1 7 - # 1 9   w i l l   d e a l   w i t h   Q U I T   e v e n t .   I n   t h i s   c a s e ,   p y g a m e   w i l l   b e   t e r m i n a t e d   t h e n   s y s t e m   w i l l   b e   t e r m i n a t e d   b e c a u s e   s y s t e m   m u s t   b e   t e r m i n a t e d   a f t e r   p y g a m e   i s   t e r m i n a t e d !   ( O t h e r   e v e n t s   w i l l   b e   e x p l a i n e d   l a t e r . ) 
 
 
 
 R e g a r d i n g   t h i s   t e m p l a t e   i s   f i x e d ,   t h e n   w e   c a n   a d d   s p e c i a l   f u n c t i o n s   i n   c o r r e c t   p l a c e   t o   p r i n t    H e l l o   W o r l d !  .   F i r s t ,   w e   n e e d   t o   s e t   * * f o n t * *   a n d   * * s i z e * * .   ` ` p y g a m e . f o n t . F o n t (  H o o n W h i t e C a t R , t t f  ,   3 2 ) ` `   f u n c t i o n   a t   # 9   w i l l   s e t   n o t   o n l y   f o n t   b y   t t f   f i l e   b u t   a l s o   s i z e   ( 3 2 ) .   R e t u r n   v a l u e   o f   t h i s   f u n c t i o n   n e e d   t o   b e   s t o r e d   i n t o   o b j e c t ( =   m y T e x t F o n t ) .   T h e n   m e m b e r   f u n c t i o n   o f   m y T e x t F o n t   n a m e d   ` ` r e n d e r (  H e l l o   W o r l d !  ,   T r u e ,   r e d ,   g r e e n ) ` `   a t   # 1 0   w i l l   r e t u r n   a   v a l u e ( =   m y T e x t ) .   r e n d e r   f u n c t i o n   c a n   s e t   * * t e x t   c o n t e n t s * *   a n d   * * c o l o r * * ( r e d   i s   c o l o r   o f   t e x t ,   g r e e n   i s   c o l o r   o f   a r e a   o u t s i d e   o f   t e x t ) .   T h e n   m e m b e r   f u n c t i o n   o f   m y T e x t   n a m e d   ` ` g e t _ r e c t ( ) ` `   a t   # 1 1   w i l l   r e t u r n   a   v a l u e ( =   m y T e x t A r e a ) .   m y T e x t A r e a   m e a n s   t h e   a r e a   a l l o c a t e d   f o r   p r i n t i n g   t e x t .   W h e n   g e t _ r e c t ( )   i s   c a l l e d ,   a   r e c t a n g l e   a r e a   i s   r e t u r n e d   c o r r e s p o n d i n g   t o   t e x t  s   l e n g t h   a n d   f o n t   s i z e .   N o w ,   a l l   w e   h a v e   t o   d o   i s   l o c a t i n g   t h i s   a r e a   s o m e w h e r e .   I f   w e   c h a n g e   m e m b e r   v a l u e   o f   m y T e x t A r e a   n a m e d   ` ` c e n t e r ` `   a t   # 1 2   i n t o   c e n t e r   o f   s c r e e n ,   t h e n   t h e   t e x t  s   c e n t e r   * * l o c a t i o n * *   w i l l   b e   c e n t e r   o f   t h e   s c r e e n . 
 
 
 
 B u t   h o w   t o   d e t e r m i n e   w h e r e   i s   t h e   c e n t e r   o f   s c r e e n ?   F i r s t ,   w e   h a v e   t o   d e c i d e   t h e   r a n g e   o f   s c r e e n ,   b y   u s i n g   ` ` p y g a m e . d i s p l a y . s e t _ m o d e ( ( 6 4 0 , 4 8 0 ) ) ` `   i n   # 8 ,   c a n v a s ( w h e r e   a l l   v a l u e s   w h i c h   h a s   s i z e ,   c o l o r ,   p o s i t i o n   w i l l   b e   d r a w n   w h e n   d i s p l a y . u p d a t e   i s   c a l l e d )   w i l l   b e   g e n e r a t e d   a n d   i t s   s i z e   b e c o m e s   6 4 0   x   4 8 0 .   T h e n ,   i t s   c e n t e r   i s   e x a c t l y   ( 3 2 0 , 2 4 0 ) .   I f   t h e   t o t a l   s i z e   i s   g i v e n ,   w e   c a n   d e c i d e   a n y   p o s i t i o n   e v e n   a c c o u n t i n g   s o m e   m a r g i n   w i t h   l i t t l e   c a l c u l a t i o n   ( R e m i n d   t h a t   i n   2 D   G U I ,   e v e r y t h i n g   h a s   x   a n d   y   w h i c h   h a s   t o   b e   p r i n t e d )   ( J u s t   l i k e   t u r t l e   g r a p h i c ,   i f   i t   g o e s   * * r i g h t ,   x   i n c r e a s e s * * ,   i f   i t   g o e s   * * d o w n * * ,   * * y   i n c r e a s e * * .   D o n  t   c o n f u s e d ! ) .   A n d   w e   k n o w   t h a t   a l l   f u n c t i o n s   I   m e n t i o n e d   w i l l   b e   p u t t e d   i n t o   I n i t i a l   s t a t e m e n t ,   b e c a u s e   t h i s   i n f o r m a t i o n   a r e   f i x e d   w h i l e   t h e   p r o g r a m   g o   o n . 
 
 A l t h o u g h   e v e r y t h i n g   i s   f i x e d ,   w e   h a v e   t o   d e s i g n   t h a t   f i l l   a n d   b l i t   f u n c t i o n s   t o   b e   p u t t e d   i n t o   A l w a y s   s t a t e m e n t ,   b e c a u s e   o f   t h e s e   f u n c t i o n s    t r a i t .   ` ` f i l l ( w h i t e ) ` `   f u n c t i o n   i n   # 1 4   m e a n s   f i l l i n g   c a n v a s   b y   s i n g l e   c o l o r ( w h i t e ) .   ` ` b l i t ( m y T e x t ,   m y T e x t A r e a ) ` `   f u n c t i o n   i n   # 1 5   m e a n s   d r a w i n g   s p e c i f i c   o b j e c t ( =   m y T e x t )   o n   s p e c i f i c   l o c a t i o n ( =   m y T e x t A r e a )   o f   c a n v a s .   N o t i c e   t h a t   d r a w i n g ( b l i t )   h a s   t o   b e   d o n e   a f t e r   f i l l i n g ( f i l l ) .   W h e n   e v e r y t h i n g   i s   d r a w n   o n   c a n v a s ,   r e s u l t   o f   c a n v a s   w i l l   b e   d i s p l a y   o n t o   w i n d o w   w h e n   d i s p l a y . u p d a t e   i s   e x e c u t e d . 
 
 
 
 T h a t   w a s   t h e   e x p l a n a t i o n   o f   t h e   e n t i r e   s o u r c e   c o d e ,   w h i c h   h a s   2 0   l i n e s .   I t   s e e m s   i t   t a k e s   t o o   m u c h   t i m e   t o   u n d e r s t a n d   s o u r c e   c o d e   w i t h   o n l y   2 0   l i n e s .   H o w e v e r ,   a d d i n g   o r   c h a n g i n g   f r o m   t h i s   s o u r c e   c o d e   i s   n o t   t h a t   h a r d   b e c a u s e   w e   u n d e r s t a n d   t h e   t e m p l a t e   o f   t h i s   s o u r c e   c o d e   a n d   s t e p   o f   p r i n t i n g .   W h a t   a b o u t   a d d i n g   p r o c e s s   l o g i c   i n   t h i s   s o u r c e   c o d e ?   T h a t   w i l l   b e   n e x t   p r o j e c t . 
 
 
 
 < R e f e r e n c e   C o d e >   : : 
 
 
 
         i m p o r t   p y g a m e ,   s y s   # 1 
 
         f r o m   p y g a m e . l o c a l s   i m p o r t *   # 2 
 
 
 
         w h i t e   =   ( 2 5 5 , 2 5 5 , 2 5 5 )   # 3 
 
         r e d   =   ( 2 5 5 , 0 , 0 )   # 4 
 
         g r e e n   =   ( 0 , 2 5 5 , 0 )   # 5 
 
         p y g a m e . i n i t ( )   # 6 
 
         p y g a m e . d i s p l a y . s e t _ c a p t i o n ( " H e l l o   W o r l d   P r o j e c t " )   # 7 
 
         m y S c r e e n   =   p y g a m e . d i s p l a y . s e t _ m o d e ( ( 6 4 0 ,   4 8 0 ) )   # 8 
 
         m y T e x t F o n t   =   p y g a m e . f o n t . F o n t ( " H o o n W h i t e c a t R . t t f " ,   3 2 )   # 9 
 
         m y T e x t   =   m y T e x t F o n t . r e n d e r ( " H e l l o   W o r l d ! " ,   T r u e ,   r e d ,   g r e e n )   # 1 0   
 
         m y T e x t A r e a   =   m y T e x t . g e t _ r e c t ( )   # 1 1 
 
         m y T e x t A r e a . c e n t e r   =   ( 3 2 0 ,   2 4 0 )   # 1 2 
 
 
 
         w h i l e   T r u e :   # 1 3 
 
                 m y S c r e e n . f i l l ( w h i t e )   # 1 4 
 
                 m y S c r e e n . b l i t ( m y T e x t ,   m y T e x t A r e a )   # 1 5 
 
 
 
                 f o r   e v e n t   i n   p y g a m e . e v e n t . g e t ( ) :   # 1 6 
 
                         i f   e v e n t . t y p e   = =   Q U I T :   # 1 7 
 
                                 p y g a m e . q u i t ( )   # 1 8 
 
                                 s y s . e x i t ( )   # 1 9 
 
 
 
                 p y g a m e . d i s p l a y . u p d a t e ( )   # 2 0 
 
 
 
 